package com.xoa.controller.BBS;

import com.xoa.model.BBS.BbsCommentWithBLOBs;
import com.xoa.model.department.Department;
import com.xoa.model.users.Users;
import com.xoa.service.BBS.BbsCommentService;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.users.UsersService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//讨论区
@Controller
@RequestMapping("/bbs")
public class BbsCommentController {

    @Autowired
    private BbsCommentService bbsCommentService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UsersService usersService;

    //讨论区映射
    @RequestMapping("/index")
    public String bbsLordindex(){
        return "/app/bbs/bbsLord/index";
    }

    //讨论区主题映射
    @RequestMapping("/theme")
    public String theme(){

        return "/app/bbs/bbsLord/theme";
    }

    //讨论区主题帖子审核页面
    @RequestMapping("/bbsCheck")
    public String bbsCheck(){
        return "/app/bbs/bbsLord/bbsCheck";
    }

    //讨论区全局公告页面 和 发帖
    @RequestMapping("/allNotice")
    public String allNotice(){
        return "app/bbs/bbsLord/allNotice";
    }

    //文章搜索结果页面
    @RequestMapping("/searchResult")
    public String searchResult(){
        return "/app/bbs/bbsLord/searchResult";
    }

    //积分榜页面
    @RequestMapping("/gradeTop")
    public String gradeTop(){
        return "/app/bbs/bbsLord/gradeTop";
    }

    //热门文章页面
    @RequestMapping("/hotArticles")
    public String hotArticles(){
        return "/app/bbs/bbsLord/hotArticles";
    }

    //文章页面
    @RequestMapping("/article")
    public String article(){
        return "/app/bbs/bbsLord/article";
    }


    //文章回复功能页面
    @RequestMapping("/revert")
    public String revert(){
        return "/app/bbs/bbsLord/revert";
    }



    /**
     * 添加讨论区内容
     * @param record
     * @return
     */
    @RequestMapping("/addCom")
    @ResponseBody
    public ToJson insertSelective(HttpServletRequest request,BbsCommentWithBLOBs record) {
        return bbsCommentService.insertSelective(request,record);
    }

    /**
     * 删除讨论区内容及评论和回复
     * @param
     * @return
     */
    @RequestMapping("/delCom")
    @ResponseBody
    public ToJson deleteAnyById(Integer[] commentIds) {
        return bbsCommentService.deleteAnyById(commentIds);
    }

    /**
     * 修改讨论区内容
     * @param record
     * @return
     */
    @RequestMapping("/updateCom")
    @ResponseBody
    public ToJson updateByPrimaryKeySelective(HttpServletRequest request,BbsCommentWithBLOBs record) {
        return bbsCommentService.updateByPrimaryKeySelective(request,record);
    }

    //根据ID查询返回父级和子级
    @RequestMapping("/selectById")
    @ResponseBody
    public ToJson selectById(HttpServletRequest request,Integer id) {
        return bbsCommentService.selectById(request,id);
    }

    /**
     * 批量修改审批状态
     * @param ids
     * @param isCheck
     * @return
     */
    @RequestMapping("/updatebatch")
    @ResponseBody
    public ToJson updatebatch(HttpServletRequest request,Integer[] ids,Integer isCheck) {
        return bbsCommentService.updatebatch(request,ids,isCheck);
    }

    /**
     * 加精和置顶接口
     * @param request
     * @param ids
     * @return
     */
    @RequestMapping("/updatefine")
    @ResponseBody
    public ToJson updatefine(HttpServletRequest request,String[] ids,String Field,String status) {
        return bbsCommentService.updatefine(request,ids,Field,status);
    }

    /**
     * 查询最后发表人讨论区内容
     * @param
     * @return
     */
    @RequestMapping("/selLastPerson")
    @ResponseBody
    public ToJson selLastPublicPerson(int boardId) {
        return bbsCommentService.selLastPublicPerson(boardId);
    }

    /**
     * 查询讨论区内的主题
     * @param
     * @return
     */
    @RequestMapping("/selAllCom")
    @ResponseBody
    public ToJson selAllCommentById(Integer boardId,Integer page,Integer limit) {
        return bbsCommentService.selAllCommentById(boardId,page,limit);
    }

    /**
     * 查询讨论区帖子审核
     * @param
     * @return
     */
    @RequestMapping("/selReview")
    @ResponseBody
    public ToJson selReview(HttpServletRequest request,Integer boardId,Integer page,Integer limit) {
        return bbsCommentService.selReview(request,boardId,page,limit);
    }

    /**
     * 搜索功能
     * @param
     * @return
     */
    @RequestMapping("/selByCon")
    @ResponseBody
    public ToJson selByCon(BbsCommentWithBLOBs record) {
        return bbsCommentService.selByCon(record);
    }

    /**
     * 积分榜
     * @param
     * @return
     */
    @RequestMapping("/selTextNums")
    @ResponseBody
    public ToJson selTextNums() {
        return bbsCommentService.selTextNums();
    }

    /**
     * 查询内容和回复
     * @param
     * @return
     */
    @RequestMapping("/selContentAndReply")
    @ResponseBody
    public ToJson selContentAndReply(HttpServletRequest request,Integer commentId){
        return bbsCommentService.selContentAndReply(request,commentId);
    }

    /**
     * 查询热门文章
     * @param
     * @return
     */
    @RequestMapping("/selHotPost")
    @ResponseBody
    public ToJson selHotPost() {
        return bbsCommentService.selHotPost();
    }

    //查询转移主题查询接口
    @RequestMapping("/Transfer")
    @ResponseBody
    public ToJson Transfer(Integer boardId) {
        return bbsCommentService.findTransfer(boardId);
    }

    //转移主题接口
    @RequestMapping("/updateTransfer")
    @ResponseBody
    public ToJson updateTransfer(Integer[] commentId,Integer boardId) {
        return bbsCommentService.updateTransfer(commentId,boardId);
    }


    //讨论区阅
    @ResponseBody
    @RequestMapping(value = "/getChDeptfq", produces = {"application/json;charset=UTF-8"})
    public ToJson<Department> getChDept(HttpServletRequest request,@RequestParam(value = "id",required = true) Integer id) {
       /* String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);*/
        ToJson<Department> json = new ToJson<Department>(0, null);
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

            List<Users> userByuserId=new ArrayList();
            //查找文章的已看人员
            ToJson toJson = bbsCommentService.selectById(request, id);
            if (toJson.isFlag()){
                Map objectMap =(Map<String,Object>) toJson.getObject();
                BbsCommentWithBLOBs thislevel = (BbsCommentWithBLOBs)objectMap.get("Thislevel");
                if (thislevel!=null){
                    String readeder = thislevel.getReadeder();
                    if (!StringUtils.checkNull(readeder)){
                        userByuserId = usersService.getUserByuserId(readeder);
                    }
                }
            }



            List<Department> departmentByClassifyOrg = departmentService.getDepartmentByClassifyOrg(users, false, false );
            List<Department> childList = getChildList(departmentByClassifyOrg, userByuserId);


            json.setObj(childList);
            json.setMsg("OK");
            json.setFlag(0);
            return json;
        } catch (Exception e) {
            json.setMsg(e.getMessage());
        }
        return json;
    }

    public static List<Department> getChildList(List<Department> list,  List<Users> userByuserId) {
        for (Department department : list) {

            Map map=new HashMap();
            //每个部门查找是否有已读人员
            for (Users users1:userByuserId ) {
                if (department.getDeptId()==users1.getDeptId() || department.getDeptId().equals(users1.getDeptId())){
                    map.put(users1.getUserId(),users1.getUserName());
                }
            }
            department.setMap(map);
            if (department.getChild()!=null && department.getChild().size()>0) {//存在子集
                getChildList(department.getChild(),userByuserId);
            }
        }
        return list;
    }

}
